package capricornus.components.alert
{
	import capricornus.components.ComponentsUtils;
	import capricornus.components.button.Button;
	import capricornus.components.label.Label;
	import capricornus.components.window.FrameWindow;
	import capricornus.components.window.WindowAlignType;
	import capricornus.core.StageManager;
	import capricornus.events.StageEvent;
	import capricornus.res.DefaultResManager;
	import capricornus.utils.shake.ShakeController;
	import capricornus.utils.shake.ShakeManager;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	internal class AlertView extends FrameWindow
	{
		/**
		 * 模态对话框的时候才有 
		 */		
		private var _back:Sprite;
		
		private var _id:int;
		
		private var _txt:Label;
		private var _btn:Button;
		
		private var _cancelFun:Function;
		private var _okFun:Function;
		
		public function AlertView( owner:DisplayObjectContainer )
		{
			super(owner);
			init();
		}
		
		private function init():void
		{
			if( !SET_DEFAULT_WINDOW_STYLE )
			{
				_windowWidth = 220;
				_windowHeight = 150;
				setDefaultStyle();
			}
			
			_txt = new Label();
			_txt.textColor = 0xFF9900;
			_txt.setSize( 180, 66 );
			_txt.x = (_windowWidth - _txt.width)>>1;
			_txt.y = 40;
			addChild( _txt );
			
			_btn = ComponentsUtils.createButton( "确定", 66, 22 );
			_btn.x = (_windowWidth - _btn.width)>>1;
			_btn.y = 122;
			addChild( _btn );
			
			setAlignType( WindowAlignType.SCALE, WindowAlignType.SCALE );
			_btn.addEventListener( MouseEvent.CLICK, onClickOK );
		}
		
		override public function destroy():void
		{
			super.destroy();
			_txt.destroy();
			if( _btn != null )
			{
				_btn.removeEventListener( MouseEvent.CLICK, onClickOK );
				_btn.destroy();
				_btn = null;
			}
			if( _back != null )
			{
				_back.parent.removeChild( _back );
				(_back.getChildAt(0) as Bitmap).bitmapData = null;
				_back.removeEventListener( MouseEvent.CLICK, onClickBack );
				_back = null;
				
				StageManager.instance.removeEventListener( StageEvent.RESIZE, onResize ); 
			}
		}
		
		public function setInfo( id:int, msg:String, title:String, ok:Function, cancel:Function, model:Boolean ):void
		{
			_id = id;
			
			_txtTitle.htmlText = title;
			_txt.htmlText = msg;
			
			_okFun = ok;
			_cancelFun = cancel;
			
			if( model )
			{
				_back = new Sprite();
				_back.addChild( DefaultResManager.instance.defaultRectBitmap );
				_owner.addChildAt( _back, 0 );
				onResize(null);
				_back.addEventListener( MouseEvent.CLICK, onClickBack );
				StageManager.instance.addEventListener( StageEvent.RESIZE, onResize ); 
			}
		}
		
		private function onClickBack( e:MouseEvent ):void
		{
			if( e.target==_back )
			{
				ShakeManager.shake( this );
			}
		}
		
		private function onResize( e:StageEvent ):void
		{
			_back.width = StageManager.stageWidth;
			_back.height = StageManager.stageHeight;
		}
		
		private function onClickOK( e:MouseEvent ):void
		{
			if( _okFun != null ) _okFun.apply();
			super.close();
		}
		
		override public function close():void
		{
			if( _cancelFun != null ) _cancelFun.apply();
			super.close();
			
			Alert.destroy( _id );
		}
	}
}





